source ~/.vim/plug/plug.vim
source ~/.vim/plug/gtags.vim
source ~/.vim/plug/gtags-cscope.vim

set fileencoding=utf-8
set fileencodings=utf-8,ucs-bom,cp936
let g:mapleader=','
set t_Co=256

call plug#begin("~/.vim/plugged")
Plug 'vim-airline/vim-airline'
Plug 'vim-airline/vim-airline-themes'
Plug 'tomasr/molokai'
Plug 'junegunn/vim-easy-align'
Plug 'scrooloose/nerdtree', { 'on':  'NERDTreeToggle' }
Plug 'fatih/vim-go', { 'tag': '*' }
Plug 'altercation/vim-colors-solarized'
Plug 'airblade/vim-gitgutter'
Plug 'ctrlpvim/ctrlp.vim'
call plug#end()

" Common {{{
set ai                          " set auto-indenting on for programming
set showmatch                   " automatically show matching brackets. works like it does in bbedit.
set vb                          " turn on the "visual bell" - which is much quieter than the "audio blink"
set ruler                       " show the cursor position all the time
set laststatus=2                " make the last line where the status is two lines deep so you can see status always
set backspace=indent,eol,start  " make that backspace key work the way it should
set nocompatible                " vi compatible is LAME
set showmode                    " show the current mode
set clipboard=unnamed           " set clipboard to unnamed to access the system clipboard under windows

set backspace=2
set noswapfile
set nobackup
set noundofile
set number
set ambiwidth=double
set shiftwidth=4
set softtabstop=4
set tabstop=4
set autoindent
set cindent
set expandtab
syntax on
set background=dark
"color solarized
"color vividchalk
"color dracula
color molokai
"set guifont=Courier_new:h11::cDEFAULT
set guifont=Consolas:h10::cDEFAULT
set guioptions-=T
set guioptions-=m
set guioptions-=L
set guioptions-=r
set showtabline=2
set linespace=2
set completeopt=longest,menu
set completeopt-=preview
set cursorline
set hlsearch
set nowrap
set foldmethod=syntax
set nofoldenable

set wildignore+=*/tmp/*,*.so,*.swp,*.zip
set wildignore+=*\\tmp\\*,*.swp,*.zip,*.exe

function! TrimEndLinesAndTailSpace()
    let save_cursor = getpos(".")
    :silent! %s#\($\n\s*\)\+\%$##
    exe "normal mz"
    %s/\s\+$//ge
    exe "normal `z"
    call setpos('.', save_cursor)
endfunction
autocmd BufWritePre *.c,*.h,*.py call TrimEndLinesAndTailSpace()
" }}}

" Key map {{{
map <C-Tab> gt
map <C-S-Tab> gT
" }}}

" minitty course {{{
let &t_ti.="\e[1 q"
let &t_SI.="\e[5 q"
let &t_EI.="\e[1 q"
let &t_te.="\e[0 q"
" }}}

" airline {{{
set noshowmode
set timeoutlen=300
set laststatus=2
let g:airline_powerline_fonts = 0
let g:airline_theme = "wombat"
let g:airline_extensions = [ 'ctrlp', 'hunks' ]
let g:airline#extensions#whitespace#enabled = 0
let g:airline#extensions#tabline#enabled = 0
" }}}

" scrooloose/nerdtree {{{
map <F7> :NERDTreeToggle<CR>
" }}}

" ctrlpvim/ctrlp.vim {{{
let g:ctrlp_custom_ignore = '\v[\/]\.(git|hg|svn)$'
let g:ctrlp_custom_ignore = {
  \ 'dir':  '\v[\/]\.(git|hg|svn)$',
  \ 'file': '\v\.(exe|so|dll)$',
  \ 'link': 'some_bad_symbolic_links',
  \ }
map <leader>f :CtrlP<CR>
map <leader>s :CtrlPBufTag<CR>
" }}}


"  whatot/gtags-cscope.vim {{{
let $GTAGSROOT=getcwd()
let $GTAGSDBPATH=$TMP . "/GTAGS/" . fnamemodify(getcwd(), ":s?.*[\\\/]??")
"let $GTAGSDBPATH=$GTAGSROOT
let g:Gtags_OpenQuickfixWindow=0
let GtagsCscope_Auto_Load=1

set cscopetag
set csprg=gtags-cscope

function! GenerateGTAGS()
    call mkdir($GTAGSDBPATH, "p", 0700)
    execute "!gtags " . $GTAGSDBPATH
    execute "cs add ". $GTAGSDBPATH . "/GTAGS"
endfunction

function! GlobalComplete(findstart, base)
    if a:findstart == 1
        let l:line = getline('.')
        let l:start = col('.') - 1
        while l:start > 0 && ( l:line[l:start - 1] =~# '\a' || l:line[l:start - 1] =~# '_' || l:line[l:start - 1] =~# '\d')
            let l:start -= 1
        endwhile
        return l:start
    else
        let l:rets = system('global -c ' . a:base)
        let l:rets = substitute(l:rets, '\r\n', '\n', 'g')
        return split(l:rets)
    endif
endfunction

function! CFileFormat()
    let l:line = line('.')
    let l:col = col('.')
    execute '%!astyle --ascii -U -A14 -s4 -Y -pH -k3 -j -z2 -m0'
    call cursor(l:line, l:col)
endfunction

autocmd FileType c,h,cpp,hpp,cc noremap <F3> <ESC>:call CFileFormat()<CR>
autocmd FileType c,h,cpp,hpp,cc noremap <F4> :call GenerateGTAGS()<CR>
autocmd FileType c,h,cpp,hpp,cc setlocal omnifunc=GlobalComplete
nmap <C-\>s :cs find s <C-R>=expand("<cword>")<CR><CR>
nmap <C-\>g :cs find g <C-R>=expand("<cword>")<CR><CR>
nmap <C-\>c :cs find c <C-R>=expand("<cword>")<CR><CR>
nmap <C-\>d :cs find d <C-R>=expand("<cword>")<CR><CR>

" }}}
